<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @copyright Copyright 2008 Rodrigo Rutkoski Rodrigues
 */

/**
 *
 * @author Rodrigo Rutkoski Rodrigues <rutkoski@gmail.com>
 *
 */
class HTMLElement
{

  /**
   * Parent element
   *
   * @var HTMLElement
   */
  public $parent;

  /**
   * The element's name
   *
   * @var string
   */
  protected $element;

  /**
   * Element's attributes
   *
   * @var Dictionary
   */
  protected $attribs;

  /**
   * Extra parameters
   *
   * @var array|Dictionary
   */
  protected $params;

  /**
   * Elements rendered before this element
   *
   * @var array
   */
  protected $before = array();

  /**
   * Elements rendered inside this element
   *
   * @var array
   */
  protected $children = array();

  /**
   * Elements rendered after this element
   *
   * @var array
   */
  protected $after = array();

  /**
   * Tags that do not have a closing tag.
   *
   * @var array
   */
  protected $closedElements = array(
    'img', 'input', 'button', 'hr', 'link', 'meta', 'br'
  );

  /**
   * Constructor.
   *
   * @param $element string The element's name (p, td, table, br, ...)
   * @param $attribs string|array The tag's attributes.
   * @param $params array Extra parameters.
   * @return void
   */
  public function __construct($element, $attribs = null, $params = null)
  {
    $this->element = $element;

    $this->attribs = Dictionary::parseFrom($attribs);
    $this->params = Dictionary::parseFrom($params);

    $this->params->set('closeTag', in_array($element, $this->closedElements));

    $args = func_get_args();
    array_splice($args, 0, 3);

    foreach ($args as $arg) {
      $this->append($arg);
    }
  }

  /**
   * Shortcut for creating a new HTMLElement.
   *
   * @param $element string The element's name (p, td, table, br, ...)
   * @param $attribs string|array The tag's attributes.
   * @param $params array Extra parameters.
   * @return HTMLElement
   */
  public static function render($element, $attribs = null, $params = null)
  {
    $e = new HTMLElement($element, $attribs, $params);

    $args = func_get_args();
    array_splice($args, 0, 3);

    foreach ($args as $arg) {
      $e->append($arg);
    }

    return $e;
  }

  /**
   *
   * @return HTMLElement
   */
  public function attr($name, $value = null)
  {
    if (is_null($value)) {
      return $this->attribs->get($name);
    }

    $this->attribs->set($name, $value);

    return $this;
  }

  /**
   *
   * @return HTMLElement
   */
  public function removeAttr($name)
  {
    $this->attribs->del($name);

    return $this;
  }

  /**
   * Append a new child inside the element.
   *
   * @param $element string|HTMLElement
   * @return HTMLElement
   */
  public function append()
  {
    $args = func_get_args();

    foreach ($args as $element) {
      if ($element instanceof HTMLElement) {
        $element->parent = $this;
      }

      array_push($this->children, $element);
    }

    return $this;
  }

  /**
   * Append a new child inside the element.
   *
   * @param $element string|HTMLElement
   * @return HTMLElement
   */
  public function prepend($element)
  {
    if ($element instanceof HTMLElement) {
      $element->parent = $this;
    }

    array_unshift($this->children, $element);

    return $this;
  }

  /**
   * Append a new element after this element.
   *
   * @param $element string|HTMLElement
   * @return HTMLElement
   */
  public function after($element)
  {
    if ($element instanceof HTMLElement) {
      $element->parent = $this->parent;
    }

    array_push($this->after, $element);

    return $this;
  }

  /**
   * Append a new element before this element.
   *
   * @param $element string|HTMLElement
   * @return HTMLElement
   */
  public function before($element)
  {
    if ($element instanceof HTMLElement) {
      $element->parent = $this->parent;
    }

    array_unshift($this->before, $element);

    return $this;
  }

  /**
   * Wrap a new HTMLElement around this element.
   *
   * @param $element string The element's name (p, td, table, br, ...)
   * @param $attribs string|array The tag's attributes.
   * @param $params array Extra parameters.
   * @return HTMLElement The new (wrapping) element.
   */
  public function wrap($element, $attribs = null, $params = null)
  {
    $e = new HTMLElement($element, $attribs, $params, $this);

    $this->parent = $e;

    foreach ($this->before as $element) {
      $element->parent = $e;
    }

    foreach ($this->after as $element) {
      $element->parent = $e;
    }

    return $e;
  }

  /**
   * Render the element.
   *
   * @return string
   */
  public function renderHtml()
  {
    $before = '';
    foreach ($this->before as $e) {
      $before .= $e;
    }

    $children = '';
    foreach ($this->children as $e) {
      $children .= $e;
    }

    $template = $this->params->get('closeTag') ? '%4$s<%1$s%2$s/>%5$s' : '%4$s<%1$s%2$s>%3$s</%1$s>%5$s';

    $_attribs = array();

    foreach ($this->attribs->getAll() as $name => $value) {
      $_attribs[] = sprintf('%s="%s"', $name, $value);
    }

    $_attribs = implode(' ', $_attribs);

    if (strlen($_attribs)) {
      $_attribs = ' ' . $_attribs;
    }

    $after = '';
    foreach ($this->after as $e) {
      $after .= $e;
    }

    $output = sprintf($template, $this->element, $_attribs, $children, $before, $after);

    return $output;
  }

  /**
   * Automagically renders the element.
   *
   * @return string
   */
  public function __toString()
  {
    return $this->renderHtml();
  }

}

/**
 *
 * @return HTMLElement
 */
function e($element, $attribs = null, $params = null)
{
  if (! ($element instanceof HTMLElement)) {
   $element = new HTMLElement($element, $attribs, $params);
  }

  return $element;
}

?>